package com.basic.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
	private BroadcastReceiver mQuitReceiver;//退出监听器
	private BroadcastReceiver mNetReceiver;//网络状态监听器
	private Toast mToast;

	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		private static final long timeOut = 3000;
		private long screenOnTime;
		private final Handler handler = new Handler();

		@Override
		public void onReceive(final Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						//dosometing
					}
				}, timeOut);
			} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
				screenOnTime = System.currentTimeMillis();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerScreenOffReceive();
	}

	private void registerScreenOffReceive() {
		if (isAutoCloseWhenScreenOff()) {
			IntentFilter filter = new IntentFilter();
			filter.addAction(Intent.ACTION_SCREEN_OFF);
			filter.addAction(Intent.ACTION_SCREEN_ON);
			registerReceiver(mReceiver, filter);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterScreenOffReceiver();
		unregisterNetReceiver();
	}

	private void unregisterScreenOffReceiver() {
		if (isAutoCloseWhenScreenOff()) {
			unregisterReceiver(mReceiver);
		}
	}


	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mToast != null) {
			mToast.cancel();
		}
	}

	public void showToast(int resId) {
		if (mToast == null) {
			mToast = Toast.makeText(this, resId, Toast.LENGTH_SHORT);
		} else {
			mToast.cancel();
			mToast.setText(resId);
		}
		mToast.show();
	}

	public void showToast(String text) {
		if (mToast == null) {
			mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		} else {
			mToast.cancel();
			mToast.setText(text);
		}
		mToast.show();
	}

	protected boolean isAutoCloseWhenScreenOff() {
		return true;
	}

	/**
	 * 网络变化时调用，应该主动调用registerNetReceiver
	 * 
	 * @param isNetValid
	 */
	protected void onNetChanged(boolean isNetValid) {
	}

	/**
	 * 监听网络状态
	 */
	protected final void registerNetReceiver() {
		mNetReceiver = new BroadcastReceiver() {
			private boolean isFirst = true;

			@Override
			public void onReceive(Context context, Intent intent) {
				if (isFirst) {
					isFirst = false;
					return;
				}
				Log.d("netconfig", "net changed" + intent.getExtras());
			}
		};
		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(mNetReceiver, filter);
	}

	protected final void unregisterNetReceiver() {
		if (mNetReceiver != null) {
			unregisterReceiver(mNetReceiver);
			mNetReceiver = null;
		}
	}
}
